package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.Episode;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.le123.ysdq.R;

import java.util.List;

public class PlayerOnLineAdapter extends BaseAdapter {

    private Context mContext;

    private List<Episode> mOnLineEpisode;

    private LayoutInflater mInflater;

    private String mType;
    
    /**
     * 用户所选剧集的porder
     */
    private String mSelectProder;

    public PlayerOnLineAdapter(Context context, String type, List<Episode> onLineEpisode) {
        mContext = context;
        mType = type;
        mOnLineEpisode = onLineEpisode;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        if (null != mOnLineEpisode) {
            return mOnLineEpisode.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (null != mOnLineEpisode && position <= (mOnLineEpisode.size() - 1)) {
            return mOnLineEpisode.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class VideoHolder {
        TextView count;

        TextView period_count;

        TextView name;

        LinearLayout varietyLayout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VideoHolder holder = null;
        if (convertView == null) {
            holder = new VideoHolder();
            convertView = mInflater.inflate(R.layout.videoplayer_playcontroller_selecr_model, null);
            holder.count = (TextView) convertView.findViewById(R.id.count);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.period_count = (TextView) convertView.findViewById(R.id.period_count);
            holder.varietyLayout = (LinearLayout) convertView.findViewById(R.id.varietylayout);
            convertView.setTag(holder);
        } else {
            holder = (VideoHolder) convertView.getTag();
        }

        if (MoviesConstant.VT_ZONGYI.equals(mType)) {
            setVarietyData(holder, position);
        } else {
            setPeriodData(holder, position);
        }
        setSelectTextColor(holder, position);
        return convertView;
    }

    private void setSelectTextColor(VideoHolder holder, int position) {
        Episode episode = mOnLineEpisode.get(position);
        if (mSelectProder.equals(episode.getPorder())) {
            holder.count.setSelected(true);
            holder.name.setSelected(true);
            holder.period_count.setSelected(true);
        } else {
            holder.count.setSelected(false);
            holder.name.setSelected(false);
            holder.period_count.setSelected(false);
        }
    }

    /**
     * 设置多集数据 zhangshuo 2014年7月5日 下午4:49:16
     */
    private void setPeriodData(VideoHolder holder, int position) {
        holder.varietyLayout.setVisibility(View.GONE);
        holder.count.setVisibility(View.VISIBLE);
        if (null != mOnLineEpisode) {
            Episode episode = mOnLineEpisode.get(position);
            if (!TextUtils.isEmpty(episode.getPorder())) {

                String currCount = null;
                if (mType.equals(MoviesConstant.VT_MOVIE)) {
                    currCount = mContext.getString(R.string.videoplayer_duan, episode.getPorder());
                } else {
                    currCount =
                            mContext.getString(R.string.videoplayer_periods, episode.getPorder());
                }
                // String currCount = String.format(mContext.getResources()
                // .getString(R.string.videoplayer_periods), (episode
                // .getPorder()));
                holder.count.setText(currCount);
            }
        }
    }

    /**
     * 设置综艺数据
     * 
     * @param holder zhangshuo 2014年7月5日 下午4:49:40
     */
    private void setVarietyData(VideoHolder holder, int position) {
        holder.varietyLayout.setVisibility(View.VISIBLE);
        holder.count.setVisibility(View.GONE);
        if (null != mOnLineEpisode) {
            Episode episode = mOnLineEpisode.get(position);
            String porder = episode.getPorder();
            String name = episode.getSubName();
            if(TextUtils.isEmpty(name)){
                name = episode.getName();
            }
            if (!TextUtils.isEmpty(porder)) {
                holder.period_count.setVisibility(View.VISIBLE);
                holder.period_count.setText(porder);
            } else {
                holder.period_count.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(name)) {
                holder.name.setVisibility(View.VISIBLE);
                holder.name.setText(name);
            } else {
                holder.name.setVisibility(View.GONE);
            }
        }
    }

    public List<Episode> getmOnLineEpisode() {
        return mOnLineEpisode;
    }

    public void setmOnLineEpisode(List<Episode> mOnLineEpisode) {
        this.mOnLineEpisode = mOnLineEpisode;
    }
    
    public String getmSelectProder() {
        return mSelectProder;
    }

    public void setmSelectProder(String mSelectProder) {
        this.mSelectProder = mSelectProder;
    }

}
